package com.xuexiang.xqrcode.ui;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.xuexiang.xqrcode.R;
import com.xuexiang.xqrcode.XQRCode;
import com.xuexiang.xqrcode.camera.CameraManager;
import com.xuexiang.xqrcode.decoding.CaptureViewHandler;
import com.xuexiang.xqrcode.decoding.InactivityTimer;
import com.xuexiang.xqrcode.util.QRCodeAnalyzeUtils;
import com.xuexiang.xqrcode.view.ViewfinderView;
import java.io.IOException;
import java.util.Vector;

/* loaded from: classes2.dex */
public class CaptureFragment extends Fragment implements ICaptureView, SurfaceHolder.Callback {
    private static final float BEEP_VOLUME = 0.1f;
    private static final long VIBRATE_DURATION = 200;
    private final MediaPlayer.OnCompletionListener beepListener = new MediaPlayer.OnCompletionListener() { // from class: com.xuexiang.xqrcode.ui.CaptureFragment.1
        @Override // android.media.MediaPlayer.OnCompletionListener
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.seekTo(0);
        }
    };
    CameraInitCallBack callBack;
    private QRCodeAnalyzeUtils.AnalyzeCallback mAnalyzeCallback;
    private Camera mCamera;
    private String mCharacterSet;
    private Vector<BarcodeFormat> mDecodeFormats;
    private CaptureViewHandler mHandler;
    private boolean mHasSurface;
    private InactivityTimer mInactivityTimer;
    private boolean mIsRepeated;
    private MediaPlayer mMediaPlayer;
    private boolean mPlayBeep;
    private long mScanInterval;
    private SurfaceHolder mSurfaceHolder;
    private SurfaceView mSurfaceView;
    private boolean mVibrate;
    private ViewfinderView mViewfinderView;

    /* loaded from: classes2.dex */
    public interface CameraInitCallBack {
        void callBack(Exception exc);
    }

    @Override // android.view.SurfaceHolder.Callback
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    }

    @Override // com.xuexiang.xqrcode.ui.ICaptureView
    public /* bridge */ /* synthetic */ Activity getActivity() {
        return super.getActivity();
    }

    public static CaptureFragment newInstance(int i) {
        return newInstance(i, false, 0L);
    }

    public static CaptureFragment newInstance(int i, boolean z, long j) {
        if (i == -1) {
            return null;
        }
        CaptureFragment captureFragment = new CaptureFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(XQRCode.KEY_LAYOUT_ID, i);
        bundle.putBoolean(XQRCode.KEY_IS_REPEATED, z);
        bundle.putLong(XQRCode.KEY_SCAN_INTERVAL, j);
        captureFragment.setArguments(bundle);
        return captureFragment;
    }

    public static void onCreate(Activity activity) {
        if (activity != null) {
            activity.getWindow().addFlags(128);
            activity.getWindow().setFormat(-3);
        }
    }

    @Override // android.support.v4.app.Fragment
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        onCreate(getActivity());
        CameraManager.init(((FragmentActivity) requireNonNull(getActivity())).getApplicationContext());
        this.mHasSurface = false;
        this.mInactivityTimer = new InactivityTimer(getActivity());
    }

    @Override // android.support.v4.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View view;
        Bundle arguments = getArguments();
        if (arguments != null) {
            int i = arguments.getInt(XQRCode.KEY_LAYOUT_ID);
            view = i != -1 ? layoutInflater.inflate(i, (ViewGroup) null) : null;
            this.mIsRepeated = arguments.getBoolean(XQRCode.KEY_IS_REPEATED);
            this.mScanInterval = arguments.getLong(XQRCode.KEY_SCAN_INTERVAL);
        } else {
            view = null;
        }
        if (view == null) {
            view = layoutInflater.inflate(R.layout.xqrcode_fragment_capture, (ViewGroup) null);
        }
        this.mViewfinderView = (ViewfinderView) view.findViewById(R.id.viewfinder_view);
        this.mSurfaceView = (SurfaceView) view.findViewById(R.id.preview_view);
        this.mSurfaceHolder = this.mSurfaceView.getHolder();
        return view;
    }

    @Override // android.support.v4.app.Fragment
    public void onResume() {
        super.onResume();
        if (this.mHasSurface) {
            initCamera(this.mSurfaceHolder);
        } else {
            this.mSurfaceHolder.addCallback(this);
            this.mSurfaceHolder.setType(3);
        }
        this.mDecodeFormats = null;
        this.mCharacterSet = null;
        this.mPlayBeep = true;
        AudioManager audioManager = (AudioManager) ((FragmentActivity) requireNonNull(getActivity())).getSystemService("audio");
        if (audioManager != null && audioManager.getRingerMode() != 2) {
            this.mPlayBeep = false;
        }
        initBeepSound();
        this.mVibrate = true;
    }

    @Override // android.support.v4.app.Fragment
    public void onPause() {
        super.onPause();
        CaptureViewHandler captureViewHandler = this.mHandler;
        if (captureViewHandler != null) {
            captureViewHandler.quitSynchronously();
            this.mHandler = null;
        }
        CameraManager.get().closeDriver();
    }

    @Override // android.support.v4.app.Fragment
    public void onDestroyView() {
        this.mInactivityTimer.shutdown();
        super.onDestroyView();
    }

    @Override // com.xuexiang.xqrcode.ui.ICaptureView
    public void handleDecode(Result result, Bitmap bitmap) {
        CaptureViewHandler captureViewHandler;
        this.mInactivityTimer.onActivity();
        playBeepSoundAndVibrate();
        if (result == null || TextUtils.isEmpty(result.getText())) {
            QRCodeAnalyzeUtils.AnalyzeCallback analyzeCallback = this.mAnalyzeCallback;
            if (analyzeCallback != null) {
                analyzeCallback.onAnalyzeFailed();
            }
        } else {
            QRCodeAnalyzeUtils.AnalyzeCallback analyzeCallback2 = this.mAnalyzeCallback;
            if (analyzeCallback2 != null) {
                analyzeCallback2.onAnalyzeSuccess(bitmap, result.getText());
            }
        }
        if (!this.mIsRepeated || (captureViewHandler = this.mHandler) == null) {
            return;
        }
        captureViewHandler.sendEmptyMessageDelayed(R.id.restart_preview, this.mScanInterval);
    }

    private void initCamera(SurfaceHolder surfaceHolder) {
        try {
            CameraManager.get().openDriver(surfaceHolder);
            this.mCamera = CameraManager.get().getCamera();
            CameraInitCallBack cameraInitCallBack = this.callBack;
            if (cameraInitCallBack != null) {
                cameraInitCallBack.callBack(null);
            }
            if (this.mHandler == null) {
                this.mHandler = new CaptureViewHandler(this, this.mDecodeFormats, this.mCharacterSet, this.mViewfinderView);
            }
        } catch (Exception e) {
            CameraInitCallBack cameraInitCallBack2 = this.callBack;
            if (cameraInitCallBack2 != null) {
                cameraInitCallBack2.callBack(e);
            }
        }
    }

    @Override // android.view.SurfaceHolder.Callback
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        if (this.mHasSurface) {
            return;
        }
        this.mHasSurface = true;
        initCamera(surfaceHolder);
    }

    @Override // android.view.SurfaceHolder.Callback
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        this.mHasSurface = false;
        if (this.mCamera == null || !CameraManager.get().isPreviewing()) {
            return;
        }
        if (!CameraManager.get().isUseOneShotPreviewCallback()) {
            this.mCamera.setPreviewCallback(null);
        }
        this.mCamera.stopPreview();
        CameraManager.get().getPreviewCallback().setHandler(null, 0);
        CameraManager.get().getAutoFocusCallback().setHandler(null, 0);
        CameraManager.get().setPreviewing(false);
    }

    @Override // com.xuexiang.xqrcode.ui.ICaptureView
    public Handler getCaptureHandler() {
        return this.mHandler;
    }

    @Override // com.xuexiang.xqrcode.ui.ICaptureView
    public void drawViewfinder() {
        this.mViewfinderView.drawViewfinder();
    }

    private void initBeepSound() {
        if (this.mPlayBeep && this.mMediaPlayer == null) {
            ((FragmentActivity) requireNonNull(getActivity())).setVolumeControlStream(3);
            this.mMediaPlayer = new MediaPlayer();
            this.mMediaPlayer.setAudioStreamType(3);
            this.mMediaPlayer.setOnCompletionListener(this.beepListener);
            AssetFileDescriptor openRawResourceFd = getResources().openRawResourceFd(R.raw.beep);
            try {
                this.mMediaPlayer.setDataSource(openRawResourceFd.getFileDescriptor(), openRawResourceFd.getStartOffset(), openRawResourceFd.getLength());
                openRawResourceFd.close();
                this.mMediaPlayer.setVolume(0.1f, 0.1f);
                this.mMediaPlayer.prepare();
            } catch (IOException unused) {
                this.mMediaPlayer = null;
            }
        }
    }

    private void playBeepSoundAndVibrate() {
        Vibrator vibrator;
        MediaPlayer mediaPlayer;
        if (this.mPlayBeep && (mediaPlayer = this.mMediaPlayer) != null) {
            mediaPlayer.start();
        }
        if (!this.mVibrate || (vibrator = (Vibrator) ((FragmentActivity) requireNonNull(getActivity())).getSystemService("vibrator")) == null) {
            return;
        }
        vibrator.vibrate(VIBRATE_DURATION);
    }

    public QRCodeAnalyzeUtils.AnalyzeCallback getAnalyzeCallback() {
        return this.mAnalyzeCallback;
    }

    public void setAnalyzeCallback(QRCodeAnalyzeUtils.AnalyzeCallback analyzeCallback) {
        this.mAnalyzeCallback = analyzeCallback;
    }

    public void setCameraInitCallBack(CameraInitCallBack cameraInitCallBack) {
        this.callBack = cameraInitCallBack;
    }

    public static <T> T requireNonNull(T t) {
        if (t != null) {
            return t;
        }
        throw new NullPointerException();
    }
}
